<?php
namespace app\admin\collection;
use OSS\Core\OssException;
use OSS\OssClient;
use think\Config;

class AliyunOss
{
    private  $accessKeyId;
    private  $accessKeySecret;
    private  $endpoint;
    public $bucket;
    private  $isCName;


    public  function __construct($isCName=true)
    {
        $this->accessKeyId = Config::get('aliyun.accessKeyId');
        $this->accessKeySecret = Config::get('aliyun.accessKeySecret');

        if($isCName===true){
            $this->endpoint = Config::get('aliyun.custom');
        }else{
            $this->endpoint = Config::get('aliyun.endpoint_out');
        }

        $this->bucket = Config::get('aliyun.bucket');
        $this->isCName = $isCName;
    }


    public  function upload($bucket, $object, $content){
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->isCName);
          return  $ossClient->putObject($this->bucket, $object, $content);
        } catch (OssException $e) {
            print $e->getMessage();
        }
    }

    public function createBucket ($bucket){
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->createBucket($bucket);
        } catch (OssException $e) {
            print $e->getMessage();
        }
    }

    public function getCurlofimg($bucketName,$object,$timeout=3600){


        $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
        /**
         *  生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒
         */

        $waterurl ='logo/guwanwater.png?x-oss-process=image/resize,P_20';
        $waterurl = rtrim(strtr(base64_encode($waterurl), '+/', '-_'), '=');
        $options = array(
            OssClient::OSS_PROCESS => 'image/resize,m_fixed,h_700,w_700/sharpen,100/watermark,image_'.$waterurl.',t_90,g_se,x_10,y_10',
        );
        $signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, "GET", $options);

        return  $signedUrl;
    }


    public function nomatergetCurlofimg($bucketName,$object,$timeout=3600){
        $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
        /**
         *  生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒v
         */

        $options = array(
            OssClient::OSS_PROCESS => 'image/resize,m_fill,h_160,w_160/sharpen,100',
        );
        $signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, 'GET', $options);

        return  $signedUrl;
    }


    /*
     * 身份证水印图片
     *
     */
    public function getnomatergetCurlofimgverfity($bucketName,$object,$timeout=3600){
        $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
        /**
         *  生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒
         */

        $waterurl =Config::get('aliyunwatertextweixin.yongtu');
        $waterurl = rtrim(strtr(base64_encode($waterurl), '+/', '-_'), '=');


        $fontType ='fangzhengshusong';
        $fontType = rtrim(strtr(base64_encode($fontType), '+/', '-_'), '=');

        $options = array(
            OssClient::OSS_PROCESS => 'image/resize,m_fixed,h_1200,w_1200/sharpen,100/watermark,type_'.$fontType.',size_40,text_'.$waterurl.',color_FFFFFF,size_30,rotate_315,t_90,g_center,x_10,y_10',
        );
        $signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, "GET", $options);

        return  $signedUrl;
    }


    public  function uploadFile($bucket, $object, $file_name){
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->isCName);
            return  $ossClient->uploadFile($bucket, $object, $file_name);
        } catch (OssException $e) {
              $e->getMessage();
            return false;
        }
    }

}